﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.ContractManagement;
using WaterCloud.Domain.SystemOrganize;
using System.Runtime;
using System.Collections;
using WaterCloud.Domain.InfoManage;
namespace WaterCloud.Service.ContractManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-03-31 10:37
    /// 描 述：合同维护服务类
    /// </summary>
    public class ContractMaintainService : BaseService<ContractMaintainEntity>, IDenpendency
    {
        public ContractMaintainService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<ContractMaintainEntity>> GetList(string keyword = "")
        {
            var data = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_EnCode.Contains(keyword)
                
                || a.F_EnCode.Contains(keyword));
            }
            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_CreatorTime , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<ContractMaintainEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_EnCode.Contains(keyword));
            }
             //权限过滤
             query = GetDataPrivilege("a", "", query);
             return await query.OrderBy(a => a.F_CreatorTime , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<ContractMaintainEntity>> GetLookList(SoulPage<ContractMaintainEntity> pagination,bool b=false,string keyword = "",string id="")
        {

           
            var query = repository.Db.Queryable<ContractMaintainEntity, UserEntity,OrganizeEntity>((a, a1,a2) => new JoinQueryInfos(
                JoinType.Left, a.F_UserId == a1.F_Id,
                  JoinType.Left, a1.F_OrganizeId== a2.F_Id
                )).Where((a, a1,a2) => a.F_DeleteMark == b)
                .Select((a, a1,a2) => new ContractMaintainEntity
                {
                    F_Id = a.F_Id.SelectAll(),
                    F_Organize=a2.F_FullName,

                }).MergeTable();
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_EnCode.Contains(keyword)
              
                || a.F_EnCode.Contains(keyword));
            }
            if(!string.IsNullOrEmpty(id))
            {
                query= query.Where(a=>a.F_Id==id);
            }
            //权限过滤
            query = GetDataPrivilege("a","",query);
            return  await query.ToPageListAsync(pagination);
        }

        public async Task<ContractMaintainEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return data;
        }
        #endregion

        public async Task<ContractMaintainEntity> GetLookForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            string[] temp;
            if (data.F_UserId != null)
            {
                temp = data.F_UserId.Split(',');
                data.F_RealName = string.Join(",", repository.Db.Queryable<UserEntity>().Where(a => temp.Contains(a.F_Id)).Select(a => a.F_RealName).ToList().ToArray());
            }
            return GetFieldsFilterData(data);
        }
        public async Task<List<ContractMaintainEntity>> GetWaring1(SoulPage<ContractMaintainEntity> pagination,string keyword = "")
        {
            var query = repository.Db.Queryable<ContractMaintainEntity, UserEntity, OrganizeEntity>((a, a1, a2) => new JoinQueryInfos(
                JoinType.Left, a.F_UserId == a1.F_Id,
                  JoinType.Left, a1.F_OrganizeId == a2.F_Id
                )).Where(a => a.F_DeleteMark == false && a.F_EnabledMark == true && SqlFunc.DateDiff(DateType.Day, DateTime.Now, Convert.ToDateTime(a.F_EndDate)) < 60)
                .Select((a, a1, a2) => new ContractMaintainEntity
                {
                    F_Id = a.F_Id.SelectAll(),
                    F_Organize = a2.F_FullName,

                }).MergeTable().OrderBy(a => a.F_CreatorTime);

            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改

                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_EnCode.Contains(keyword));
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.ToPageListAsync(pagination);
        
        }
        public async Task<List<ContractMaintainEntity>> GetWaring2(SoulPage<ContractMaintainEntity> pagination, string keyword = "")
        {

            var query = repository.Db.Queryable<ContractMaintainEntity, UserEntity, OrganizeEntity>((a, a1, a2) => new JoinQueryInfos(
                JoinType.Left, a.F_UserId == a1.F_Id,
                  JoinType.Left, a1.F_OrganizeId == a2.F_Id
                )).Where((a, a1,a2) => a.F_DeleteMark == false && a1.F_WorkState == 2)
                .Select((a, a1, a2) => new ContractMaintainEntity
                {
                    F_Id = a.F_Id.SelectAll(),
                    F_Organize = a2.F_FullName,

                }).MergeTable();

            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改

                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_EnCode.Contains(keyword));
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.ToPageListAsync(pagination);

        }
        #region 提交数据
        public async Task SubmitForm(ContractMaintainEntity entity, string keyValue)
        {
            


            if(string.IsNullOrEmpty(keyValue))
            {
                    //初始值添加
                entity.F_DeleteMark = false;
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                    //此处需修改
                entity.Modify(keyValue); 
                await repository.Update(entity);
            }
        }
        public async Task CloseForm(string keyValue)
        {
          
            var ids = keyValue.Split(',');
            await repository.Update(a => ids.Contains(a.F_Id), a => new ContractMaintainEntity
            {
                F_EnabledMark = false,
         
            });
        }
        public async Task DeleteForm(string keyValue)
        {
            //var ids = keyValue.Split(',');
            //await repository.Delete(a => ids.Contains(a.F_Id.ToString()));

            var ids = keyValue.Split(',');
            await repository.Update(a => ids.Contains(a.F_Id), a => new ContractMaintainEntity
            {
                F_EnabledMark = false,
                F_DeleteMark = true,
                F_DeleteTime = DateTime.Now,
                F_DeleteUserId =currentuser.UserId,
            });
        }

        
        #endregion

    }
}
